package com.creditharmony.car.loan.service.impl;

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.creditharmony.car.common.base.service.impl.BaseServiceImpl;
import com.creditharmony.car.common.common.vo.requestvo.CarLoanCustomerRequestVo;
import com.creditharmony.car.common.common.vo.responsevo.CarLoanCustomerResponseVo;
import com.creditharmony.car.common.exception.UserException;
import com.creditharmony.car.common.exception.UserExceptionConstants;
import com.creditharmony.car.common.utils.UuidUtil;
import com.creditharmony.car.loan.dao.CarLoanCustomerDao;
import com.creditharmony.car.loan.entity.CarLoanCustomerEntity;
import com.creditharmony.car.loan.service.CarLoanCustomerService;
/**
 * 
 * @Class Name CarLoanCustomerServiceImpl
 * @author zqa
 * @Create In 2018年1月26日
 */
@Service
public class CarLoanCustomerServiceImpl extends BaseServiceImpl<CarLoanCustomerEntity> implements CarLoanCustomerService{
    
    private static final Logger logger = LoggerFactory.getLogger(CarLoanCustomerServiceImpl.class);
            
    /** 客户个人信息数据服务. */
    @Autowired
    private CarLoanCustomerDao carLoanCustomerDao;

    /**
     * 客户个人信息录入.
     * 
     * @param requestVo 请求参数(客户个人信息)
     */
    public void insertCarCustomer(CarLoanCustomerRequestVo requestVo) {
        CarLoanCustomerEntity entity = new CarLoanCustomerEntity();
        try {
            BeanUtils.copyProperties(requestVo, entity);
        } catch (BeansException e) {
            logger.error("CarLoanCustomerEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR);
        }
        String id = UuidUtil.getUUID();
        entity.setId(id);
        entity.setCreateTime(new Date());
        try {
            carLoanCustomerDao.insertCarCustomer(entity);
        } catch (Exception e) {
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR);
        }
    }

    /**
     * 获取单条数据
     * @param loanCode 借款编号 
     * @return 客户个人信息查询结果
     */
    public CarLoanCustomerResponseVo selectByLoanCode(String loanCode) {
        CarLoanCustomerResponseVo reponseVo = new CarLoanCustomerResponseVo();
        CarLoanCustomerEntity entity;
        try {
            entity = carLoanCustomerDao.selectByLoanCode(loanCode);
        } catch (Exception e) {
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR);
        }
        try {
            BeanUtils.copyProperties(entity, reponseVo);
        } catch (BeansException e) {
            logger.error("CarLoanCustomerEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR);
        }
        return reponseVo;
    }

    /**
     * 更新客户个人信息
     * @param requestVo 请求参数(客户个人信息数据)
     */
    public void updateCarLoanCustomer(CarLoanCustomerRequestVo requestVo) {
        CarLoanCustomerEntity entity = new CarLoanCustomerEntity();
        try {
            BeanUtils.copyProperties(requestVo, entity);
        } catch (BeansException e) {
            logger.error("CarLoanCustomerEntity对象拷贝属性值抛出异常",e);
            throw new UserException(UserExceptionConstants.BEAN_COPY_ERROR);
        }
        entity.setModifyTime(new Date());
        try {
            carLoanCustomerDao.updateCarLoanCustomer(entity);
        } catch (Exception e) {
            logger.error(UserExceptionConstants.DB_SYSTEM_ERROR, e);
            throw new UserException(UserExceptionConstants.DB_SYSTEM_ERROR);
        }
    }
}
